import { editInvitation, findByAgentBySelectList } from '@/services/agent';
import { findByGoodsBySelectList } from '@/services/goods';
import { getResult } from '@/services/sys/attribute';
import { PlusOutlined } from '@ant-design/icons';
import { ModalForm } from '@ant-design/pro-components';
import {
  ProForm,
  ProFormMoney,
  ProFormSelect,
  ProFormSwitch,
  ProFormText,
} from '@ant-design/pro-form';
import { Button, Form, message } from 'antd';
import React, { useState } from 'react';

interface Props {
  row?: API.AgentInvitationRelationship;
  onSuccess?: () => void;
  onFail?: () => void;

  text: string;
  isCard?: boolean;
}

const valueEnumOff = {
  '1': '开',
  '0': '关',
};
const EditInvitation: React.FC<Props> = (props) => {
  const { row, onSuccess, onFail, text, isCard } = props;
  const [formRef] = Form.useForm<API.AgentInvitationRelationship>();
  //  const formRef = useRef<ProFormInstance<API.AgentInvitationRelationship>> ();
  const [params, setParams] = useState<API.AgentInvitationRelationship>();
  const [agentInvitationRelationship, setAgentInvitationRelationship] =
    useState<API.AgentInvitationRelationship>(row);
  return (
    <ModalForm<API.AgentInvitationRelationship>
      form={formRef}
      initialValues={agentInvitationRelationship}
      title={'产品起订量配置'}
      trigger={
        isCard ? (
          <Button
            type="primary"
            onClick={() => {
              formRef.resetFields();
            }}
          >
            <PlusOutlined />
            {text}
          </Button>
        ) : (
          <Button
            type="link"
            onClick={() => {
              formRef.resetFields();
            }}
          >
            <PlusOutlined />
            {text}
          </Button>
        )
      }
      onFinish={(values) => {
        return new Promise<Boolean>((resolve, reject) => {
          editInvitation(values)
            .then(() => {
              message.success('操作成功');
              formRef.resetFields();
              //   refresh ();
              if (onSuccess) {
                onSuccess();
              }
              resolve(true);
            })
            .catch(() => {
              if (onFail) {
                onFail();
              }
              message.error('操作失败');
              reject(false);
            });
        });
      }}
    >
      <ProFormText hidden={true} width="md" name="id" />
      <ProFormText hidden={true} width="md" name="categoryId" />

      <ProFormText hidden={true} width="md" name="isPrice" />
      <ProFormText hidden={true} width="md" name="isLevel" />
      <ProFormText hidden={true} width="md" name="isParent" />
      <ProForm.Group>
        <ProFormSelect
          name="agentId"
          key="agentId"
          rules={[{ required: true }]}
          request={findByAgentBySelectList}
          width="sm"
          label="推荐人等级"
        />
        <ProFormSelect
          name="relationAgentId"
          key="relationAgentId"
          request={findByAgentBySelectList}
          width="sm"
          label="下单代理等级"
        />
        <ProFormSelect
          name="goodsId"
          request={findByGoodsBySelectList}
          width="sm"
          key="goodsId"
          label="产品名称"
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormMoney name="price" width="sm" label="代理门槛" />
        <ProFormMoney name="upgradePrice" width="sm" label="升级提成" />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormSwitch
          width="sm"
          label="代理门槛%"
          onChange={(value: boolean) => {
            //console.log(value);
            if (value) {
              formRef.setFieldsValue({
                isPrice: 1,
              });
            } else {
              formRef.setFieldsValue({
                isPrice: 0,
              });
            }
          }}
        />
        <ProFormSwitch
          width="sm"
          label="商品上级提成%"
          onChange={(value: boolean) => {
            //console.log(value);
            if (value) {
              formRef.setFieldsValue({
                isParent: 1,
              });
            } else {
              formRef.setFieldsValue({
                isParent: 0,
              });
            }
          }}
        />
        <ProFormSwitch
          width="sm"
          label="代理等级%"
          onChange={(value: boolean) => {
            //console.log(value);
            if (value) {
              formRef.setFieldsValue({
                isLevel: 1,
              });
            } else {
              formRef.setFieldsValue({
                isLevel: 0,
              });
            }
          }}
        />
        {/*        <ProFormSwitch name="isParent" width="sm" label="商品上级提成%" />
        <ProFormSwitch name="isLevel" width="sm" label="代理等级%" />*/}
      </ProForm.Group>
      <ProForm.Group>
        <ProFormMoney name="level1" width="sm" label="一级返利" />
        <ProFormMoney name="level2" width="sm" label="二级返利" />
        <ProFormMoney name="level3" width="sm" label="三级返利" />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormSelect
          name="currentUpgradeAgentId"
          key="currentUpgradeAgentId"
          request={findByAgentBySelectList}
          width="sm"
          label="当前的等级"
        />
        <ProFormSelect
          name="upgradeAgentId"
          key="upgradeAgentId"
          request={findByAgentBySelectList}
          width="sm"
          label="升级等级"
        />
        <ProFormMoney name="parentPrice" width="sm" label="商品上级提成" />
        <ProFormSelect
          name="state"
          key="state"
          rules={[{ required: true }]}
          request={() => {
            return getResult('agentInvitationRelationship', 1);
          }}
          width="sm"
          label="状态"
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormMoney name="reduceUpgradePrice" width="sm" label="升级减" />
        <ProFormSwitch
          width="sm"
          label="活动"
          onChange={(value: boolean) => {
            //console.log(value);
            if (value) {
              formRef.setFieldsValue({
                isActivty: 1,
              });
            } else {
              formRef.setFieldsValue({
                isActivty: 0,
              });
            }
          }}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default EditInvitation;
